import { wmsStore } from '@/store/pinia'

const hasRole = (roles: any) => {
    const pinia_roles: any = wmsStore().roles
    if (typeof roles == 'string') {
        return pinia_roles.includes(roles)
    } else if (Array.isArray(roles)) {
        return pinia_roles.some((item: any) => roles.includes(item))
    }

    return false;
}

const hasPermission = (permissions: any) => {
    const pinia_permissions: any = wmsStore().Permissions
    if (typeof permissions == 'string') {
        return pinia_permissions.includes(permissions)
    } else if (Array.isArray(permissions)) {
        return pinia_permissions.some((item: any) => permissions.includes(item))
    }

    return false;
}

export default {
    mounted(el: any, binging: any) {

        let type = binging.arg
        let value = binging.value;
        if (value == undefined) {
            return;
        }
        if (type == 'role') {
            //如不pinia里面没有这个角色就不显示
            if (!hasRole(value)) {
                el.remove();
            }

        } else if (type == 'permission') {
            //如不pinia里面没有这个权限就不显示
            if (!hasPermission(value)) {
                el.remove();
            }
        }

    }
};